<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: xuekai
  Date: 2022/3/2
  Time: 13:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<form>
    <div class="modal-body">
            <table>
                <tr>
                    <td>商品名称</td>
                    <td><input type="text" name="name" value="${goods.name}">
                        <input type="hidden" name="id" value="${goods.id}">
                    </td>
                </tr>
                <tr>
                    <td>商品价格</td>
                    <td><input type="text" name="price" value="${goods.price}"></td>
                </tr>
                <tr>
                    <td>在售状态</td>
                    <td>
                        <input type="radio" name="isSelling" value="1" ${goods.isSelling==1?'checked':''}>未售
                        <input type="radio" name="isSelling" value="0" ${goods.isSelling==0?'checked':''}>在售
                    </td>
                </tr>
                <tr>
                    <td>商品描述</td>
                    <td><input type="text" name="title" value="${goods.title}"></td>
                </tr>
                <tr>
                    <td>商品图片</td>
                    <td>
                        <input type="file" id="file" onchange="upload()">
                        <input type="hidden" name="picture" value="${goods.picture}">
                        <img id="img" src="http://localhost/img/${goods.picture}" width="70px" height="70px"/>
                        <input type="hidden" name="fileId">
                    </td>
                </tr>
                <tr>
                    <td>图片描述</td>
                    <td>
                        <input type="button" value="上传视频" onclick="to_upload()">
                        <div id="vi">

                        </div>
                        <input type="text" name="viedo" value="${goods.viedo}">
                    </td>
                </tr>
                <tr>
                    <td>品牌</td>
                    <td>
                        <select name="brandId">
                            <option>--请选择--</option>
                            <c:forEach items="${brands}" var="each">
                                <option value="${each.id}">${each.brandName}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>类别</td>
                    <td>
                        <select name="catId">
                            <option>--请选择--</option>
                            <c:forEach items="${cats}" var="each">
                                <option value="${each.id}" ${each.id == goods.catId ? 'selected' : ''}>${each.catName}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="updateGoods()">修改</button>
            </div>
        </div>
    </div>
</div>
</form>
<script type="text/javascript">
    $(function(){
        $("[name='brandId']").val(${goods.brandId});
    })
    function to_upload() {
        $("#vi").load("/mmcrofile/toup2");
    }
    function uploadOk(fileNames){
        alert(fileNames);
        $('[name="viedo"]').val(fileNames);
    }
    function upload(fileDom) {
        let fd = new FormData();
        fd.append("file",fileDom.files[0]);
        $.ajax({
            url:"${pageContext.request.contextPath}/file/upload",
            type:"post",
            data:fd,
            dataType:"JSON",
            processData:false,
            contentType:false,
            success:function (obj) {
                if(obj.code == "200"){
                    $("#img").prop("src","http://localhost/img/"+obj.data.path);
                    $("[name='picture']").val(obj.data.path);
                    $("[name='pictureId']").val(obj.data.id);
                }else{
                    alert(obj.errorInfo);
                }
            }
        })
    }
    function updateGoods() {
        $.post("${pageContext.request.contextPath}/goods/updateGood",$("form").serialize(),function (obj) {
            if(obj.code == "200"){
                alert("修改成功");
                enterFun('${pageContext.request.contextPath}/goods/list');
            }else{
                alert("修改失败");
            }
        })
    }
</script>
